package project.ccso.top.service.impl;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.PageBean;
import project.ccso.top.dao.SchoolDao;
import project.ccso.top.dao.impl.SchoolDaoImpl;
import project.ccso.top.entity.City;
import project.ccso.top.entity.School;
import project.ccso.top.enumerate.OpenStatus;
import project.ccso.top.service.SchoolService;
import project.ccso.top.util.DateUtil;

/**
 * 高校Service的实现类
 * @author aibinxiao
 * @date 2018年8月23日 下午12:00:08
 */
public class SchoolServiceImpl implements SchoolService {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(SchoolServiceImpl.class);
	// 注入SchoolDao
	private SchoolDao schoolDao = new SchoolDaoImpl();
	
	
	
	
	
	/**
	 * 根据cityId查询所有高校Service实现类
	 * @return *
	 */
	@Override
	public List<School> selectByCityId(String cityIdStr) throws Exception {
		int cityId = 1;
		if(StringUtils.isEmpty(cityIdStr)) {
			throw new Exception("参数cityIdc转换异常或为null");
		}else {
			try {
				cityId = Integer.parseInt(cityIdStr);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("参数provinceId不是整数类型或超出int范围！");
			}	
		}
		return schoolDao.selectByCityId(cityId);
	}

	/**
	 * 分页查询所有高校的Service实现类*
	 */
	@Override
	public GridPageBean<School> findSchoolByPage(String pageNoStr, String pageSizeStr, String schoolId, String schoolName,
			String provinceId, String cityId, String startDate, String endDate) throws Exception {
		Integer pageNum = 1;
		Integer pageSize = 10;
		if(!StringUtils.isEmpty(pageNoStr) && !StringUtils.isEmpty(pageSizeStr)){
			boolean flag = true;
			try {
				pageNum = Integer.valueOf(pageNoStr);
				pageSize = Integer.valueOf(pageSizeStr);
			} catch (Exception e) {
				flag = false;
			}
			if(!flag) {
				throw new Exception("参数page或rows不是int类型或者超出int范围！");
			}
		}
		int records = schoolDao.countSchByParams(schoolId, schoolName, provinceId,cityId,startDate,endDate);
		GridPageBean<School> result = new GridPageBean<School>(pageNum, records, pageSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<School> rows = schoolDao.findSchPageByParams(schoolId, schoolName, provinceId, cityId, startDate, endDate, result.getStartIndex(), records);
			result.setRows(rows);
		} else{
			List<School> rows = schoolDao.findSchPageByParams(cityId, schoolName, provinceId, startDate, endDate, endDate, result.getStartIndex(), result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pageNum);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	@Override
	public School findById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void save(String id, String name, String provinceId, String cityId) throws Exception {
		// TODO Auto-generated method stub
		
	}

	/*// 1.根据城市Id获取所有的高校Service实现类
	@Override
	public List<School> findSchoolsByCityId(Integer cityId) {
		List<School> listSchools = null;
		if (cityId != null){
		    listSchools = schoolDao.findCitysByProId(cityId);
		}
		return listSchools;
	}

	*//**
	 * 2.分页条件查询符合条件的高校集合Service实现类
	 *//*
	@Override
	public GridPageBean<School> findByPage(String pageNo, String pageSize, String id, String name, String startDate,
			String endDate, String provinceId, String cityId) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = schoolDao.countByParams(id, name, startDate, endDate, provinceId,cityId);
		GridPageBean<School> result = new GridPageBean<School>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<School> rows = schoolDao.findPageByParams(id,name,startDate,endDate,provinceId,cityId,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<School> rows = schoolDao.findPageByParams(id,name,startDate,endDate,provinceId,cityId,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	*//**
	 * 3.根据高校id获取高校信息Service实现类
	 *//*
	@Override
	public School findById(String idStr) {
		School school = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			school = schoolDao.findSchoolById(id);
		}
		return school;
	}

	*//**
	 * 4.更新或者保存高校信息Service实现类
	 *//*
	@Override
	public void save(String idStr, String name, String provinceIdStr, String cityIdStr) throws Exception {
		School school = new School();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			school = schoolDao.findSchoolById(id);
		}else{
			// id为空则是添加新的三级分类
			school.setCreateTime(DateUtil.DateToStr(new Date()));
			school.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(name)){
			school.setName(name);
		}
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			school.setProvinceId(provinceId);
		}
		if(!StringUtils.isEmpty(cityIdStr)){
			Integer cityId = Integer.valueOf(cityIdStr);
			school.setCityId(cityId);
		}
		school.setAlterTime(DateUtil.DateToStr(new Date()));
		schoolDao.saveSchool(school);
	}

	*//**
	 * 5.根据id、状态更新高校启用状态Service实现类
	 *//*
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			schoolDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			schoolDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
		}
	}*/
	
}
